package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DBHelper;
import beans.JobBean;

/**
 * Servlet implementation class ViewApplyJob
 */
@WebServlet("/ViewApplyJob")
public class ViewApplyJob extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewApplyJob() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void handViewApply(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
    try{	
    	HttpSession session=request.getSession();
    	String employee = session.getAttribute("user").toString();
    	
    	Connection con=DBHelper.getConnection();
    	PreparedStatement ps=DBHelper.getprepareStatement(con,"select * from PROVIDEJOB,applyjob "
    			+ "where applyjob.jobid=PROVIDEJOB.JOBID"
    			+ " and applyjob.employeeusername =?");
    	ps.setString(1, employee);
    	ResultSet rs=ps.executeQuery();
    	ArrayList<JobBean> job=new ArrayList<JobBean>();
    	while(rs.next()){
    		String jobid = rs.getString("jobId");
    		String company = rs.getString("company");
    		String salary = rs.getString("salary");
    		String address = rs.getString("address");
    		String companysize = rs.getString("companysize");
    		String companytype = rs.getString("companytype");
    		String jobtype = rs.getString("jobtype");
    		String provider = rs.getString("provider");
    		
    		JobBean jobs = new JobBean(jobid,company, salary, address, companysize, companytype, jobtype, provider);
    		
    		job.add(jobs);
    	}
    		request.setAttribute("job", job);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    		request.getRequestDispatcher("viewApplyJob.jsp").forward(request, response);
    	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				
		try {
			handViewApply(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handViewApply(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
